<?php
	function getVote($item=false)
	{
		$now = a_gmtTime();	
		$sql ="select id,`title`,`desc` from ".DB_PREFIX."vote where `status` = 1 and (`start_time` <= '{$now}' or `start_time`=0) and (`end_time` >= '{$now}' or `end_time`=0 ) and city_id = '".C_CITY_ID."' order by sort asc,id desc";
		$vote = $GLOBALS['db']->getRow($sql);//2011-10-03 去掉缓存 chenfq 有时间参数，不能使用数据缓存
		if(!$vote)
		{
			$sql ="select  id,`title`,`desc` from ".DB_PREFIX."vote where status = 1  and (`start_time` <= '{$now}' or `start_time`=0) and (`end_time` >= '{$now}' or `end_time`=0) and city_id = 0 order by sort asc,id desc";
			$vote = $GLOBALS['db']->getRow($sql);
		}
		if($item){
			if($vote)
			{
				$voteItems = $GLOBALS['db']->getAllCached("select * from ".DB_PREFIX."vote_item where status = 1 and vote_id = '{$vote['id']}'order by sort asc,id desc");
				foreach($voteItems as $voteItem)
				{
					$sql = "select vo.*,vg.title as group_title,vg.sort as group_sort from ".DB_PREFIX."vote_option as vo left join ".DB_PREFIX."vote_group as vg on vg.id = vo.group_id where vo.item_id = '{$voteItem['id']}' group by vo.id order by vo.sort asc,vo.id desc";
					
					$voteOptions = 	$GLOBALS['db']->getAllCached($sql);
					
					foreach($voteOptions as $voteOption)
					{
						$voteItem['groups'][$voteOption['group_id']]["id"] = $voteOption['group_id'];
						$voteItem['groups'][$voteOption['group_id']]["title"] = $voteOption['group_title'];
						$voteItem['groups'][$voteOption['group_id']]["sort"] = $voteOption['group_sort'];
						$voteItem['groups'][$voteOption['group_id']]['options'][] = $voteOption;
					}
					
					foreach($voteItem['groups'] as $key => $voteGroup)
					{
						usort($voteItem['groups'][$key]['options'],"optionsSort");
					}
					
					usort($voteItem['groups'],"groupSort");
					$vote['items'][] = $voteItem;
				}
			}
		}
		
		return $vote;
	}
?>